#import "TCCDB.h"
#import <sqlite3.h>
#import "TCCIAPModel.h"
@interface TCCDB()
{
    sqlite3 *dbPoint;
}
@end
@implementation TCCDB
static TCCDB *_tccdb;
static dispatch_once_t tccdbOnceToken;
static     sqlite3 *dbPoint;
TCCDBManager tccdbManager()
{
    TCCDBManager manager =
    {
        _db,
        _insert,
        _update,
        _deleteWithModel,
        _select,
        _closeDB
    };
    return manager;
}
static TCCDB * _db()
{
    if(!_tccdb)
    {
        dispatch_once(&tccdbOnceToken, ^{
            _tccdb = [[TCCDB alloc] init];
            openDM();
            createTable();
        });
    }
    return _tccdb;
};
static void _insert(TCCIAPModel *model)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        NSString *date = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970]];
        NSString *time = [date componentsSeparatedByString:@"."][0];
        NSString *sqlStr=[NSString stringWithFormat:@"insert into stu (zfID,goodsID,receipt,isDid,time) values ('%@','%@','%@','%@','%@')",model.zfID,model.goodsID,model.receipt,model.isDid,time];
        sqlite3_stmt *stmt = nil;
        int result=sqlite3_exec(dbPoint, [sqlStr UTF8String],nil, &stmt, nil);
        if (result==SQLITE_OK)
        {
            if (SQLITE_DONE == sqlite3_step(stmt))
            {
                DeLog(@"添加学生成功");
            }
            else
            {
                DeLog(@"添加学生成功");
            }
        }
        else
        {
            DeLog(@"添加学生失败!!!!!");
        }
    });
}
static void _update(TCCIAPModel *model)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        NSString *sqlStr= [NSString stringWithFormat:@"update stu set goodsID='%@',receipt='%@',isDid='%@' where zfID='%@'",model.goodsID,model.receipt,model.isDid,model.zfID];
        int result=sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
        if (result==SQLITE_OK)
        {
            DeLog(@"更新成功");
        }
        else
        {
            DeLog(@"更新失败");
            DeLog(@"%d",result);
        }
    });
}
static void _deleteWithModel(TCCIAPModel *model)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        NSString *sqlStr=[NSString stringWithFormat:@"delete from stu where zfID='%@'",model.zfID];
        int result=sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
        if (result==SQLITE_OK)
        {
            DeLog(@"删除成功");
        }
        else
        {
            DeLog(@"删除失败");
        }
    });
}
static void _select(NSString *isDid, TCCBlock block)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        
        NSString *sqlStr=[NSString stringWithFormat:@"select * from stu where isDid=%@",isDid];
        sqlite3_stmt *stmt=nil;
        
        NSMutableArray *arr = [NSMutableArray array];
        int result=sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
        if (result==SQLITE_OK)
        {
            
            DeLog(@"查询成功");
            while (sqlite3_step(stmt)==SQLITE_ROW)
            {
                const unsigned char *zfID_char = sqlite3_column_text(stmt, 0);
                const unsigned char *goodsID_char = sqlite3_column_text(stmt, 1);
                const unsigned char *receipt_char = sqlite3_column_text(stmt, 2);
                const unsigned char *isDid_char = sqlite3_column_text(stmt,3);
                const unsigned char *time_char = sqlite3_column_text(stmt,4);
                TCCIAPModel *model = [[TCCIAPModel alloc] init];
                if (zfID_char)
                    model.zfID = [NSString stringWithUTF8String:(const char *)zfID_char];
                if (goodsID_char)
                    model.goodsID = [NSString stringWithUTF8String:(const char *)goodsID_char];
                if (receipt_char)
                    model.receipt = [NSString stringWithUTF8String:(const char *)receipt_char];
                if (isDid_char)
                    model.isDid = [NSString stringWithUTF8String:(const char *)isDid_char];
                if (time_char)
                    model.time = [NSString stringWithUTF8String:(const char *)time_char];
                [arr addObject:model];
            }
            block(arr);
        }
        else
        {
            DeLog(@"查询失败");
            DeLog(@"%d",result);
        }
    });
}
static void _closeDB()
{
    int result=sqlite3_close(dbPoint);
    if (result==SQLITE_OK)
    {
        DeLog(@"数据库关闭成功");
    }
    else
    {
        DeLog(@"数据库关闭失败");
    }
}
void openDM()
{
    NSArray *sandBox=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath=sandBox[0];
    NSString *documentPath=[sandBoxPath stringByAppendingPathComponent:@"Receipt.sqlite"];
    int result = sqlite3_open([documentPath UTF8String], &dbPoint);
    if (result ==  SQLITE_OK)
    {
        DeLog(@"数据库打开成功 = %@",documentPath);
    }
    else
    {
        DeLog(@"数据库打开失败");
    }
}
void createTable()
{
    NSString *sqlStr=@" create table if not exists stu(zfID text primary key,goodsID text,receipt text,isDid text,time text)";
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result==SQLITE_OK)
    {
        DeLog(@"表创建成功");
    }
    else
    {
        DeLog(@"表创建失败");
    }
}
@end
